﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Istek
{
    public partial class Ozet : XForm
    {
        public Ozet()
        {
            InitializeComponent();
        }

        private void Ozet_Load(object sender, EventArgs e)
        {
            
            DataTable dt = ExecuteDataTable("GetIsteklerOzet");
            RepositoryItemGridLookUpEdit ri = GetCustomGLE(dt, "Customer", "CustomerId");
            //ri.View.Columns["CustomerId"].ColumnEdit = RI(Rp.V_Customer);

            ri.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gleCustomer.Edit.Assign(ri);

            gleCustomer.EditValue = 175;
            gleCustomer.EditValueChanged += gleCustomer_EditValueChanged;
            cmbPeryot.EditValueChanged += cmbPeryot_EditValueChanged;
            cmbGosterim.EditValueChanged += cmbGosterim_EditValueChanged;

            Rfrsh();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView2.FocusedRowChanged += gridView2_FocusedRowChanged;
            gridView1.SetViewCaption("Dönem");

        }

        void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (rel == "BrandId")
            {
                int BrandId = gridView2.GetFocusedRowCellValue("BrandId").ToInt();
                if (BrandId > 0)
                    gridView3.SetViewCaption(AppSession.Lookups.Brand.FindByBrandId(BrandId).Name + " -> ESPRESSO'ya atılan Artikeller");
                else
                    gridView3.SetViewCaption("Hangi Artikeller?");
            }
        }

        void cmbGosterim_EditValueChanged(object sender, EventArgs e)
        {
            string Peryot = gridView1.GetFocusedRowCellDisplayText("Peryot");
            Rfrsh();
            gridView1.FocusedRowHandle = gridView1.LocateByDisplayText(0, gridView1.Columns["Peryot"], Peryot);
        }

        void cmbPeryot_EditValueChanged(object sender, EventArgs e)
        {
            Rfrsh();
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();

            gridView2_FocusedRowChanged(null, null);
        }

        void gleCustomer_EditValueChanged(object sender, EventArgs e)
        {
            Rfrsh();
        }

        string rel
        {
            get
            {
                return cmbGosterim.EditValue.ToStr().StartsWith("Depodan") ? "BrandId" : "ProductId";
            }
        }

        void Rfrsh()
        {
            //gridView1.VeriyiGuncelle(true, "GetIsteklerDetay", gleCustomer.EditValue.ToInt(), cmbPeryot.EditValue.ToStr(), cmbGosterim.EditValue.ToStr());

            gridView1.Init("GetIsteklerDetay", gleCustomer.EditValue.ToInt(), cmbPeryot.EditValue.ToStr(), cmbGosterim.EditValue.ToStr());
            gridView1.AddDetailGrid(gridView2, "Peryot");
            gridView2.AddDetailGrid(gridView3, "Peryot,"+rel);
            gridView1.SetCaptions("Peryot:" + cmbPeryot.EditValue.ToStr());

            if (rel == "BrandId")
            {
                gridView2.SetViewCaption("Hangi Depodan?");
                //gridView3.SetViewCaption("Hangi Artikeller?");
            }
            else
            {
                gridView2.SetViewCaption("Hangi Artikeller?");
                gridView3.SetViewCaption("Hangi Depodan?");
            }

            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly();
            gridView3.MakeReadOnly();

            gridView2.MakeColumnInvisible("Peryot");
            gridView3.MakeColumnInvisible("Peryot", rel);
            gridView1.FormatAsNumber(0, "Qty");
            gridView2.FormatAsNumber(0, "Qty");
            gridView3.FormatAsNumber(0, "Qty");
            gridView1.AddSummary("Qty");
            gridView2.AddSummary("Qty");
            gridView3.AddSummary("Qty");

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();

            gridView2_FocusedRowChanged(null, null);
        }
    }
}
